//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.wsd.smartcarwasher.service;

import com.wsd.smartcarwasher.constans.ResultState;
import com.wsd.smartcarwasher.dao.CashApplyDao;
import com.wsd.smartcarwasher.domain.CashApply;
import com.wsd.smartcarwasher.domain.Partner;
import com.wsd.smartcarwasher.util.ReturnUtil;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;

@Service
public class CashApplyService {
    private static final Logger logger = LoggerFactory.getLogger(CashApplyService.class);
    @Resource
    private CashApplyDao cashApplyDao;
    @Resource
    private PartnerService partnerService;

    public CashApplyService() {
    }

    @Transactional
    public ModelMap applyToCash(Partner partner, CashApply cashApply) {
        if (partner.getAccount().compareTo(cashApply.getPickProfit()) < 0) {
            return ReturnUtil.Error(ResultState.NO_ENOUGH_MONEY_TO_CASH.getMsg());
        } else {
            BigDecimal leftCount = partner.getAccount().subtract(cashApply.getPickProfit());
            BigDecimal totalProfit = partner.getProfit().add(cashApply.getPickProfit());
            int changePartnerRow = this.partnerService.changePartnerAccountAndProfit(leftCount, totalProfit, partner.getId());
            if (changePartnerRow < 1) {
                return ReturnUtil.Error(ResultState.CHANGE_PARTNER_LEFT_ACCOUNT_FAIL.getMsg());
            } else {
                cashApply.setAccount(leftCount);
                cashApply.setPartnerId(String.valueOf(partner.getId()));
                cashApply.setPartnerName(partner.getRealname());
                cashApply.setStatus(1);
                cashApply.setCardNum(partner.getBankcard());
                cashApply.setCreateTime(new Date());
                int changeRow = this.cashApplyDao.createCashApply(cashApply);
                return changeRow < 1 ? ReturnUtil.Error(ResultState.FAIL_APPLY_CASH.getMsg()) : ReturnUtil.Success(ResultState.SUCCESS_APPLY_CASH.getMsg(), cashApply);
            }
        }
    }

    public List<CashApply> partnerCashApplies(String partnerId) {
        List<CashApply> cashApplies = this.cashApplyDao.partnerCashApplies(partnerId);
        return CollectionUtils.isEmpty(cashApplies) ? Collections.emptyList() : cashApplies;
    }

    public List<CashApply> allPickingRecords() {
        List<CashApply> cashApplies = this.cashApplyDao.allPickingRecords();
        return CollectionUtils.isEmpty(cashApplies) ? Collections.emptyList() : cashApplies;
    }

    public ModelMap updateCashApply(CashApply apply) {
        CashApply cashApply = this.cashApplyDao.queryCashApply(apply.getId());
        if (cashApply == null) {
            return ReturnUtil.Error(ResultState.CASH_APPLY_NOT_EXIST.getMsg());
        } else if (cashApply.getStatus() == 2) {
            return ReturnUtil.Error(ResultState.CASH_APPLY_HANDLED.getMsg());
        } else if (cashApply.getStatus() == 0) {
            return ReturnUtil.Error(ResultState.CASH_APPLY_REFUSED.getMsg());
        } else {
            if (apply.getStatus() != null && 0 == apply.getStatus()) {
                if (!this.rollbackPartnerAccount(cashApply)) {
                    return ReturnUtil.Error(ResultState.UPDATE_CASH_APPLY_STATUS_FAIL.getMsg());
                }

                apply.setHandleTime(new Date());
            }

            int changeRow = this.cashApplyDao.updateCashApply(apply);
            return changeRow < 1 ? ReturnUtil.Error(ResultState.UPDATE_CASH_APPLY_STATUS_FAIL.getMsg()) : ReturnUtil.Success(ResultState.UPDATE_CASH_APPLY_STATUS_SUCCESS.getMsg());
        }
    }

    private boolean rollbackPartnerAccount(CashApply cashApply) {
        Partner partner = this.partnerService.getPartnerById(Integer.parseInt(cashApply.getPartnerId()));
        if (partner == null) {
            return false;
        } else {
            Partner newPartner = new Partner();
            newPartner.setId(partner.getId());
            newPartner.setAccount(partner.getAccount().add(cashApply.getPickProfit()));
            newPartner.setProfit(partner.getProfit().subtract(cashApply.getPickProfit()));
            return this.partnerService.updatePartner(newPartner) > 0;
        }
    }
}

/*
package com.wsd.smartcarwasher.service;

import com.wsd.smartcarwasher.constans.Global;
import com.wsd.smartcarwasher.constans.ResultState;
import com.wsd.smartcarwasher.dao.CashApplyDao;
import com.wsd.smartcarwasher.domain.CashApply;
import com.wsd.smartcarwasher.domain.Partner;
import com.wsd.smartcarwasher.util.ReturnUtil;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class CashApplyService {
    private static final Logger logger = LoggerFactory.getLogger(CashApplyService.class);

    @Resource
    private CashApplyDao cashApplyDao;

    @Resource
    private PartnerService partnerService;


    @Transactional
    public ModelMap applyToCash(Partner partner, CashApply cashApply) {
        if (partner.getAccount().compareTo(cashApply.getPickProfit()) < 0) {
            return ReturnUtil.Error(ResultState.NO_ENOUGH_MONEY_TO_CASH.getMsg());
        }
        // 更新运营商余额和累计提现金额
        BigDecimal leftCount = partner.getAccount().subtract(cashApply.getPickProfit());
        BigDecimal totalProfit = partner.getProfit().add(cashApply.getPickProfit());
        int changePartnerRow = partnerService.changePartnerAccountAndProfit(leftCount, totalProfit, partner.getId());
        if (changePartnerRow < 1) {
            return ReturnUtil.Error(ResultState.CHANGE_PARTNER_LEFT_ACCOUNT_FAIL.getMsg());
        }
        //插入提现申请信息
        cashApply.setAccount(leftCount);
        cashApply.setPartnerId(String.valueOf(partner.getId()));
        cashApply.setPartnerName(partner.getRealname());
        cashApply.setStatus(Global.CASH_APPLY_STATUS_CREATED);
        cashApply.setCardNum(partner.getBankcard());
        cashApply.setCreateTime(new Date());
        int changeRow = cashApplyDao.createCashApply(cashApply);
        if (changeRow < 1) {
            return ReturnUtil.Error(ResultState.FAIL_APPLY_CASH.getMsg());
        }
        return ReturnUtil.Success(ResultState.SUCCESS_APPLY_CASH.getMsg(), cashApply);
    }

    */
/**
     * 查询运营商的提现申请
     *
     * @param partnerId 运营商id
     * @return 提现申请
     *//*

    public List<CashApply> partnerCashApplies(String partnerId) {
        List<CashApply> cashApplies = cashApplyDao.partnerCashApplies(partnerId);
        if (CollectionUtils.isEmpty(cashApplies)) {
            return Collections.emptyList();
        }
        return cashApplies;
    }

    */
/**
     * 查询所有的提现申请
     *
     * @return 提现申请
     *//*

    public List<CashApply> allPickingRecords() {
        List<CashApply> cashApplies = cashApplyDao.allPickingRecords();
        if (CollectionUtils.isEmpty(cashApplies)) {
            return Collections.emptyList();
        }
        return cashApplies;
    }

    */
/**
     * 更新提现申请
     *
     * @param apply 提现申请
     * @return 更新结果
     *//*

    public ModelMap updateCashApply(CashApply apply) {
        CashApply cashApply = cashApplyDao.queryCashApply(apply.getId());
        if (cashApply == null) {
            return ReturnUtil.Error(ResultState.CASH_APPLY_NOT_EXIST.getMsg());
        }
        // 提现申请状态是同意或者拒绝,就不能再修改
        if (cashApply.getStatus() == Global.CASH_APPLY_STATUS_HANDLED) {
            return ReturnUtil.Error(ResultState.CASH_APPLY_HANDLED.getMsg());
        }
        if (cashApply.getStatus() == Global.CASH_APPLY_STATUS_REFUSED) {
            return ReturnUtil.Error(ResultState.CASH_APPLY_REFUSED.getMsg());
        }
        if (apply.getStatus() != null && Global.CASH_APPLY_STATUS_REFUSED == apply.getStatus()) {
            if (!rollbackPartnerAccount(cashApply)) {
                return ReturnUtil.Error(ResultState.UPDATE_CASH_APPLY_STATUS_FAIL.getMsg());
            }
            apply.setHandleTime(new Date());
        }
        int changeRow = cashApplyDao.updateCashApply(apply);
        if (changeRow < 1) {
            return ReturnUtil.Error(ResultState.UPDATE_CASH_APPLY_STATUS_FAIL.getMsg());
        }
        return ReturnUtil.Success(ResultState.UPDATE_CASH_APPLY_STATUS_SUCCESS.getMsg());
    }

    private boolean rollbackPartnerAccount(CashApply cashApply) {
        Partner partner = partnerService.getPartnerById(Integer.parseInt(cashApply.getPartnerId()));
        if (partner == null) {
            return false;
        }
        partner.setAccount(partner.getAccount().add(cashApply.getPickProfit()));
        partner.setProfit(partner.getProfit().subtract(cashApply.getPickProfit()));
        return partnerService.updatePartner(partner) > 0;
    }
}
*/
